import json

from flask import Flask, render_template, jsonify
from flask import request
from utils import get_time, get_part_data, get_30_total,get_30_part,get_top5
from spider import get_total,get_message


app = Flask(__name__)


@app.route('/')
def hello_world():  # put application's code here
    return render_template('main.html')


@app.route('/ajax', methods=["get", 'post'])
def hello_world1():  # put application's code here
    print(request.values.get("name"))
    print(request.values.get("年龄"))
    return "1000"


@app.route('/time', methods=["get", 'post'])
def hello_world2():  # put application's code here
    return get_time()


@app.route('/total', methods=["get", 'post'])
def hello_world3():  # put application's code here
    # data = get_total_data()
    data = get_total()
    return jsonify({"currentConfirmedCount": data[0], "confirmedCount": data[1], "deadCount": data[2],
                    "curedCount": data[3]})


@app.route('/part', methods=["get", 'post'])
def hello_world4():  # put application's code here
    data = get_part_data()
    res = []
    for i in data:
        if i[0] == '黑龙江省' or i[0] == '内蒙古自治区':
            res.append({"name": i[0][0:3], "value": int(i[1])})
        else:
            res.append({"name": i[0][0:2], "value": int(i[1])})
    return jsonify({"data": res})


@app.route('/30_total', methods=["get", 'post'])
def hello_world5():  # put application's code here
    data = get_30_total()
    day, confirm, dead, heal = [], [], [], []
    for a, b, c, d in data:
        month = a[4:6]
        date = a[-2:]
        mix = month + '.' + date
        day.append(mix)
        confirm.append(b)
        dead.append(c)
        heal.append(d)
    return jsonify({"day": day, "confirmed": confirm, "dead": dead, "heal": heal})


@app.route('/30_part', methods=["get", 'post'])
def hello_world6():  # put application's code here
    data = get_30_part()
    day, confirm, dead, heal = [], [], [], []
    for a, b, c, d in data:
        month = a[4:6]
        date = a[-2:]
        mix = month + '.' + date
        day.append(mix)
        confirm.append(b)
        dead.append(c)
        heal.append(d)
    return jsonify({"day": day, "confirmed": confirm, "dead": dead, "heal": heal})


@app.route('/top_5', methods=["get", 'post'])
def hello_world7():  # put application's code here
    data = get_top5()
    city = []
    confirm = []
    for a, b in data:
        city.append(a)
        confirm.append(b)
    print(city)
    return jsonify({"city": city, "confirm": confirm})


@app.route('/message', methods=["get", 'post'])
def hello_world8():  # put application's code here
    data, href = get_message()

    return jsonify({"data": data, "href": href})


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
